using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.Globalization;

namespace WebAndLoadTestPlugins
{
    //-------------------------------------------------------------------------
    // This class creates a custom extraction rule named "Custom Extract Input"
    // The user of the rule specifies the name of an input field, and the
    // rule attempts to extract the value of that input field.
    //-------------------------------------------------------------------------
    public class CustomExtractInput : ExtractionRule
    {
        /// Specify a name for use in the user interface.
        /// The user sees this name in the Add Extraction dialog box.
        //---------------------------------------------------------------------
        public override string RuleName
        {
            get { return "Custom Extract Input"; }
        }

        /// Specify a description for use in the user interface.
        /// The user sees this description in the Add Extraction dialog box.
        //---------------------------------------------------------------------
        public override string RuleDescription
        {
            get { return "Extracts the value from a specified input field"; }
        }

        // The name of the desired input field
        private string InputFieldName;
        public string Name
        {
            get { return InputFieldName; }
            set { InputFieldName = value; }
        }

        public bool ExtractValue(HtmlDocument doc, out string value)
        {
            value = "";
            if (doc == null)
            {
                return false;
            }

            foreach (HtmlTag tag in doc.GetFilteredHtmlTags(new string[] { "input" }))
            {
                if (String.Equals(tag.GetAttributeValueAsString("name"), Name, StringComparison.InvariantCultureIgnoreCase) || 
                    String.Equals(tag.GetAttributeValueAsString("id"), Name, StringComparison.InvariantCultureIgnoreCase))
                {
                    string formFieldValue = tag.GetAttributeValueAsString("value");
                    if (formFieldValue == null)
                    {
                        formFieldValue = String.Empty;
                    }

                    // return the value
                    value = formFieldValue;
                    return true;
                }
            }
            return false;
        }

        // The Extract method.  The parameter e contains the Web test context.
        //---------------------------------------------------------------------
        public override void Extract(object sender, ExtractionEventArgs e)
        {
            string value;
            e.Success = ExtractValue(e.Response.HtmlDocument, out value);
            if (e.Success)
            {
                // add the extracted value to the Web test context
                e.WebTest.Context.Add(this.ContextParameterName, value);
                e.Success = true;
            }
            else
            {
                // If the extraction fails, set the error text that the user sees
                e.Message = String.Format(CultureInfo.CurrentCulture, "Not Found: {0}", Name);
            }
        }
    }
}

